#!/bin/bash
#
# Test the MongoDB image in OpenShift.
#
# IMAGE_NAME specifies a name of the candidate image used for testing.
# The image has to be available before this script is executed.
#

THISDIR=$(dirname ${BASH_SOURCE[0]})
TEMPLATES="$THISDIR/examples/petset"

source "$THISDIR"/test-lib-openshift.sh
source ${THISDIR}/test-lib-mongodb.sh

set -eo nounset

trap ct_os_cleanup EXIT SIGINT

ct_os_check_compulsory_vars

ct_os_enable_print_logs

function check_mongodb_os_service_connection() {
  local util_image_name=$1 ; shift
  local service_name=$1 ; shift
  local user=$1 ; shift
  local pass=$1 ; shift
  local database=$1 ; shift
  local timeout=${1:-60} ; shift || :
  local pod_ip=$(ct_os_get_service_ip ${service_name})

  : "  Service ${service_name} check ..."

  local cmd="mongo ${pod_ip}/${database} -u ${user} -p${pass} --eval 'quit()'"
  SECONDS=0

  echo -n "Waiting for ${service_name} service becoming ready ..."
  while ! docker run --rm ${util_image_name} bash -c "${cmd}" ; do
    echo -n "."
    [ ${SECONDS} -gt ${timeout} ] && echo " FAIL" && return 1
    sleep 3
  done
  echo " PASS"
  return 0
}

function test_mongodb_pure_image() {
  local image_name=$1
  local image_name_no_namespace=${image_name##*/}
  local service_name=${image_name_no_namespace}

  ct_os_new_project
  ct_os_upload_image "${image_name}"
  # Create a specific imagestream tag for the image so that oc cannot use anything else
  ct_os_upload_image "${image_name}" "$image_name_no_namespace:testing"

  ct_os_deploy_pure_image "$image_name_no_namespace:testing" \
                          --name "${service_name}" \
                          --env MONGODB_ADMIN_PASSWORD=test

  ct_os_wait_pod_ready "${service_name}" 60
  check_mongodb_os_service_connection "${image_name}" "${service_name}" admin test admin 120

  ct_os_delete_project
}

function test_mongodb_replication() {
  local image_name=${1:-centos/mongodb-${VERSION//\.}-centos7}
  local image_name_no_namespace=${image_name##*/}
  local service_name=${image_name_no_namespace}

  ADMIN_PASS=adminPass

  ct_os_new_project
  docker tag "${image_name}" "${image_name}:$VERSION"

  ct_os_deploy_template_image "$TEMPLATES/mongodb-petset-persistent.yaml" \
                              MONGODB_IMAGE="${image_name}:$VERSION" \
                              MONGODB_SERVICE_NAME="${service_name}" \
                              MONGODB_USER=testu \
                              MONGODB_PASSWORD=testp \
                              MONGODB_DATABASE=testdb \
                              MONGODB_ADMIN_PASSWORD=${ADMIN_PASS} \
                              VOLUME_CAPACITY=500M

  ct_os_wait_rc_ready "${service_name}" 600
  for pod in $(ct_os_get_pod_name mongodb); do
    ct_os_wait_pod_ready $pod 300
  done
  check_mongodb_os_service_connection "${image_name}:$VERSION" "${service_name}" admin ${ADMIN_PASS} admin
  check_mongodb_os_service_connection "${image_name}:$VERSION" "${service_name}" testu testp testdb

  ct_os_run_in_pod $(ct_os_get_pod_name mongodb | head -n 1) bash -c 'set -x
. /usr/share/container-scripts/mongodb/common.sh
. /usr/share/container-scripts/mongodb/test-functions.sh
insert_and_wait_for_replication "$(replset_addr admin)" "{a:5, b:10}"'

  ct_os_delete_project
}

ct_os_cluster_up
test_mongodb_pure_image "${IMAGE_NAME}"
test_mongodb_replication "${IMAGE_NAME}"

# test with the just built image and an integrated template
echo "Running test_mongodb_integration with ${IMAGE_NAME}"
test_mongodb_integration "${IMAGE_NAME}"

# test with a released image and an integrated template
PUBLIC_IMAGE_NAME=${PUBLIC_IMAGE_NAME:-$(ct_get_public_image_name "${OS}" "${BASE_IMAGE_NAME}" "${VERSION}")}

# Try pulling the image first to see if it is accessible
if docker pull "${PUBLIC_IMAGE_NAME}"; then
  echo "Running test_mongodb_integration with ${PUBLIC_IMAGE_NAME}"
  test_mongodb_integration "${PUBLIC_IMAGE_NAME}"
else
  echo "Warning: ${PUBLIC_IMAGE_NAME} could not be downloaded via 'docker'"
  # ignore possible failure of this test for centos images
  [ "${OS}" == "rhel7" ] && false "ERROR: Failed to pull image"
fi

# Check the imagestream
test_mongodb_imagestream

OS_TESTSUITE_RESULT=0

ct_os_cluster_down

# vim: set tabstop=2:shiftwidth=2:expandtab:

